<template>
  <div class="root">
    <div class="left">
      <div class="tool">
        <div class="tool-title">矩形</div>
        <div class="tool-content">
          <div class="form">
            <div class="inputBox">
              <label for="">x</label>
              <input type="text" v-model="x" />
            </div>
            <div class="inputBox">
              <label for="">y</label>
              <input type="text" v-model="y" />
            </div>
            <div class="inputBox">
              <label for="">width</label>
              <input type="text" v-model="width" />
            </div>
            <div class="inputBox">
              <label for="">height</label>
              <input type="text" v-model="height" />
            </div>
            <div class="inputBox">
              <label for="">fillColor</label>
              <input type="color" name="" id="" v-model="fillColor" />
            </div>
            <div class="inputBox">
              <label for="">strokeColor</label>
              <input type="color" name="" id="" v-model="strokeColor" />
            </div>
            <div class="inputBox">
              <label for="">strokeWidth</label>
              <input type="text" name="" v-model="strokeWidth" />
            </div>
            <div class="inputBox">
              <button @click="drawFillRect">drawFill</button>
              <button @click="drawStrokeRect">drawStroke</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="right">
      <canvas id="rightCanvas" height="600" width="800"></canvas>
    </div>
  </div>
</template>
<script>
import { onMounted, reactive, toRefs } from "vue";
export default {
  setup() {
    let rect = {
      x: 0,
      y: 0,
      width: 0,
      height: 0,
      fillColor: "#000",
      strokeColor: "#000",
      strokeWidth: 0,
    };

    //canvas上下文
    let c = null;
    let ctx = null;

    onMounted(() => {
      c = document.querySelector("#rightCanvas");
      ctx = c.getContext("2d");
    });

    function drawFillRect() {
      ctx.fillStyle = rect.fillColor;
      ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
    }

    function drawStrokeRect() {
      ctx.strokeStyle = rect.strokeColor;
      ctx.lineWidth = rect.strokeWidth;
      ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
    }

    let reactiveData = toRefs(reactive(rect));
    return { ...reactiveData, drawFillRect, drawStrokeRect };
  },
};
</script>
<style lang='scss' scoped>
.root {
  .left {
    float: left;
    .tool {
      width: 200px;
      .tool-title {
        text-align: center;
      }
    }
  }
  .right {
    canvas {
      border: 2px solid #000;
    }
  }
}
.inputBox {
  display: flex;
  justify-content: flex-end;
  margin: 5px;
}
label {
  text-align: right;
  width: 100px;
}
input {
  display: inline-block;
  width: 60px;
}
button {
  width: 50%;
}
</style>